#
# weapon.py -- weapon object
# 
# author: Tom "Knio" Flanagan
#         knio@h4xn3t.net
#         theknio@gmail.com
#         irc.freenode.net - Knio - #python #pygame #py
#
# this file released under Public Domain
#

from constants import *

class Weapon:
    def __init__(self, ship, **kwargs):
        self.battle = ship.battle
        self.ship = ship
        self.reld = 0
        self.reloadtime = 1
        self.range = 100
        self.damage = 1
        self.targets = []
        self.__dict__.update(kwargs)

    def reload(self):
        self.targets = []
        self.reld -= 1

    def attack(self, target):
        if self.reld > 0: return
        if self.ship.distance(target) > self.range: return
        if abs(self.ship.direction(self.ship.angle(target))) > self.angle: return 0
        self.targets.append(target)
        self.fire(target)
        self.reld = self.reloadtime
        return 1

    def fire(self, target):
        pass

class Laser(Weapon):
    def fire(self, target):
        target.hit(self, self.damage)
        
class MissileLauncher(Weapon):
    def __init__(self, ship, **kwargs):
        self.battle = ship.battle
        self.side = 1
        self.ship = ship
        self.maxa = 1.
        self.maxt = 0.1
        self.hull = 10.
        self.size = 1.
        self.life = 200.
        self.reld = 0
        self.range = 30.
        self.reloadtime = 100
        self.targets = []
        self.__dict__.update(kwargs)

    def attack(self, target):
        if self.reld > 0: return
        self.targets.append(target)
        self.fire(target)
        self.reld = self.reloadtime
        return 1        
        
    def fire(self, target):
        m = missile.Missile(self.battle, **self.ship.vector())
        m.target = target
        m.size = self.size
        m.ship = self.ship
        m.maxa = self.maxa
        m.maxt = self.maxt
        m.life = self.life
        m.range = self.range

        a = RI - (self.side%2) * PI
        self.side += 1

import missile       

    
